package zd.vrp.view;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.awt.geom.Line2D;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;

import zd.vrp.global.City;
import zd.vrp.global.GlobalInformation;
import zd.vrp.global.Route;
import zd.vrp.utils.TestUtils;

public class PlotSolution1 extends JFrame {

	public double scalerX;
	public double scalerY;

	private List<Route> routeList;
	
	public PlotSolution1(){
		this(GlobalInformation.routeList);
	}
	
	public PlotSolution1(List<Route> routeList) {
		this.routeList = routeList;
		
		int width = (int) Math.round(GlobalInformation.maxX
				- GlobalInformation.minX);
		int height = (int) Math.round(GlobalInformation.maxY
				- GlobalInformation.minY);

		scalerX = GlobalInformation.screenWidth / width;
		scalerY = GlobalInformation.screenHeight / height;

		setSize(GlobalInformation.screenWidth + 2
				* GlobalInformation.leftMargin, GlobalInformation.screenHeight
				+ 2 * GlobalInformation.topMargin);

		JLabel label = new JLabel("Optimal solution:"+TestUtils.getTotalLength(routeList));

		setLayout(new FlowLayout());

		add(label);

		setVisible(true);

		addComponentListener(new ComponentAdapter() {
			public void componentMoved(ComponentEvent e) {
				repaint();
			}

			public void componentResized(ComponentEvent e) {
				repaint();
			}
		});
		
		
		/*addWindowStateListener(new WindowStateListener() {
			public void windowStateChanged(WindowEvent e) {
				System.out.println("window is moving");
				repaint();
			}
		});
*/		
	}

	
	
	public void paint(Graphics g) {
		super.paint(g);

		List<Color> colorList = new ArrayList<Color>();
		colorList.add(Color.black);
		colorList.add(Color.blue);
		colorList.add(Color.cyan);
		colorList.add(Color.DARK_GRAY);
		colorList.add(Color.green);
		colorList.add(Color.lightGray);
		colorList.add(Color.magenta);
		colorList.add(Color.orange);
		colorList.add(Color.pink);
		colorList.add(Color.red);
		colorList.add(Color.WHITE);
		int colorSize = colorList.size();
		
		for (int i = 0; i < routeList.size(); i++) {
			int eColor = i%colorSize; 
			g.setColor(colorList.get(eColor));
			Route currentRoute = routeList.get(i);
			for (int j = 0; j < currentRoute.getCityList().size() - 1; j++) {
				City currentCity = currentRoute.getCityList().get(j);
				City nextCity = currentRoute.getCityList().get(j + 1);

				int x1 = (int) Math
						.round((currentCity.getX() - GlobalInformation.minX)
								* scalerX)
						+ GlobalInformation.leftMargin;
				int y1 = (int) Math
						.round((currentCity.getY() - GlobalInformation.minY)
								* scalerY)
						+ GlobalInformation.topMargin;

				int x2 = (int) Math
						.round((nextCity.getX() - GlobalInformation.minX)
								* scalerX)
						+ GlobalInformation.leftMargin;
				int y2 = (int) Math
						.round((nextCity.getY() - GlobalInformation.minY)
								* scalerY)
						+ GlobalInformation.topMargin;

				g.drawString(String.valueOf(currentCity.getId()), x1, y1);

				g.drawLine(x1, y1, x2, y2);
			}
		}

	}

	public static void main(String[] args) {
		PlotSolution1 ps = new PlotSolution1();

	}

}
